"""
Case Type   : GUC_ErrorLog
Case Name   : 日志的级别为log以下，修改参数debug_print_parse的值为on
Create At   : 2022/06/06
Owner       : liu-tong-8848
Description :
    1.logging_collector设置为on,log_destination设置为info，log_directory设置新的路径，
    设置log_destination值为syslog，修改系统配置文件
    2.修改参数debug_print_plan的值为on，查看参数值
    3.重启数据库生成日志信息，查看日志内容
    4.恢复参数
    5.清理残留日志    
Expect      :
    1.设置成功
    2.修改成功
    3.重启数据库成功，日志中未打印查询执行结果
    4.恢复成功
    5.清理成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class ErrorLog(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.primary_root = Node('PrimaryRoot')
        self.common = Common()
        self.constant = Constant()
        self.dir_new = os.path.join(macro.PG_LOG_PATH, 'pg_errorlog_case0092')
        self.logfile = '/var/log/postgresql_case0092'
        self.log_min_msg = 'log_min_messages'
        self.log_min_msg_default = self.common.show_param(self.log_min_msg)
        self.collector = 'logging_collector'
        self.collector_default = self.common.show_param(self.collector)
        self.log_dir = 'log_directory'
        self.log_dir_default = self.common.show_param(self.log_dir)
        self.log_des = 'log_destination'
        self.log_des_default = self.common.show_param(self.log_des)
        self.log_statement = 'log_statement'
        self.log_stat_default = self.common.show_param(self.log_statement)
        self.debug_parse = 'debug_print_parse'
        self.debug_parse_default = self.common.show_param(self.debug_parse)

    def test_error_log(self):
        text = '-----step1:前置条件:logging_collector设置为on,log_directory设置' \
               '新的路径，设置log_statement的值为all，设置log_destination值' \
               '为syslog，修改系统配置文件;expect:设置成功-----'
        if 'info' != self.log_min_msg_default:
            result = self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_min_msg}=info')
            self.assertTrue(result, '执行失败' + text)
        show_msg = self.common.show_param(self.log_min_msg)
        self.assertEquals(show_msg, 'info', '执行失败' + text)

        self.logger.info(text)
        if 'on' != self.collector_default:
            result = self.primary_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.collector}=on')
            self.assertTrue(result, '执行失败' + text)
            restart_res = self.primary_sh.restart_db_cluster()
            self.assertTrue(restart_res, '执行失败' + text)
        show_msg = self.common.show_param(self.collector)
        self.assertEquals(show_msg, 'on', '执行失败' + text)

        if self.dir_new != self.log_dir_default:
            result = self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_dir}=\'{self.dir_new}\'')
            self.assertTrue(result, '执行失败' + text)
        show_msg = self.common.show_param(self.log_dir)
        self.assertEquals(show_msg, self.dir_new, '执行失败' + text)

        if 'all' != self.log_stat_default:
            result = self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_statement}=all')
            self.assertTrue(result, '执行失败' + text)
        show_msg = self.common.show_param(self.log_statement)
        self.assertEquals(show_msg, 'all', '执行失败' + text)

        if 'syslog' != self.log_des_default:
            result = self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_des}=\'syslog\'')
            self.assertTrue(result, '执行失败' + text)
        show_msg = self.common.show_param(self.log_des)
        self.assertEquals(show_msg, 'syslog', '执行失败' + text)
        self.logger.info('-------修改系统配置文件，重启服务生效-------')
        execute_cmd = f'echo "local0.*  {self.logfile}" >> ' \
            f'/etc/*syslog.conf;service rsyslog restart'
        self.logger.info(execute_cmd)
        execute_msg = self.primary_root.sh(execute_cmd).result()
        self.logger.info(execute_msg)

        text = '-----step2:修改参数debug_print_parse的值为on,查看参数值;' \
               'expect:修改成功-----'
        self.logger.info(text)
        if 'on' != self.debug_parse_default:
            result = self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.debug_parse}=on')
            self.assertTrue(result, '执行失败' + text)
            self.logger.info('-------查看参数值：show debug_print_parse；-------')
        show_msg = self.common.show_param(self.debug_parse)
        self.assertEquals(show_msg, 'on', '执行失败' + text)

        text = f'-----step3:重启数据库生成日志信息，查看日志：{self.logfile};' \
            f'expect:重启数据库成功，日志中未打印生成的解析树-----'
        self.logger.info(text)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + text)
        self.logger.info('-------查看日志内容-------')
        cat_cmd = f"cat {self.logfile}|grep 'LOG:  parse tree'"
        self.logger.info(cat_cmd)
        cat_msg = self.primary_node.sh(cat_cmd).result()
        self.logger.info(cat_msg)
        self.assertNotIn('LOG:  parse tree', cat_msg, '执行失败:' + text)

    def tearDown(self):
        text = '-----step4:恢复参数;expect:恢复成功-----'
        self.logger.info(text)
        log_min_msg_current = self.common.show_param(self.log_min_msg)
        if log_min_msg_current != self.log_min_msg_default:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_min_msg}=\'{self.log_min_msg_default}\'')
        show_log_min_msg = self.common.show_param(self.log_min_msg)

        collector_current = self.common.show_param(self.collector)
        if collector_current != self.collector_default:
            self.primary_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.collector}={self.collector_default}')
            restart_res = self.primary_sh.restart_db_cluster()
            self.logger.info(restart_res)
        show_collector = self.common.show_param(self.collector)

        log_dir_current = self.common.show_param(self.log_dir)
        if log_dir_current != self.log_dir_default:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_dir}=\'{self.log_dir_default}\'')
        show_log_dir = self.common.show_param(self.log_dir)

        log_stat_current = self.common.show_param(self.log_statement)
        if log_stat_current != self.log_stat_default:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_statement}=\'{self.log_stat_default}\'')
        show_log_statement = self.common.show_param(self.log_statement)

        log_des_current = self.common.show_param(self.log_des)
        if log_des_current != self.log_des_default:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.log_des}=\'{self.log_des_default}\'')
        show_log_des = self.common.show_param(self.log_des)

        debug_parse_current = self.common.show_param(self.debug_parse)
        if debug_parse_current != self.debug_parse_default:
            self.primary_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.debug_parse}=\'{self.debug_parse_default}\'')
        show_debug_parse = self.common.show_param(self.debug_parse)

        self.logger.info('-------恢复配置文件-------')
        sed_cmd = 'sed -i \'$d\' /etc/*syslog.conf'
        sed_msg = self.primary_root.sh(sed_cmd).result()
        self.logger.info(sed_msg)
        restart_cmd = 'service rsyslog restart'
        restart_msg = self.primary_root.sh(restart_cmd).result()
        self.logger.info(restart_msg)
        self.logger.info('-------检查配置文件中内容被删除-------')
        check_cmd = 'cat /etc/*syslog.conf'
        check_msg = self.primary_root.sh(check_cmd).result()
        self.logger.info(check_msg)

        text_remove = '-----step5:清理残留日志文件;expect:清理成功-----'
        remove_dir_cmd = f"rm -rf {self.dir_new}"
        remove_dir_result = self.primary_node.sh(remove_dir_cmd).result()
        self.logger.info(remove_dir_result)
        remove_test_dir_cmd = f"ls {self.dir_new}"
        remove_test_dir_log = self.primary_node.sh(remove_test_dir_cmd).result()
        self.logger.info(remove_test_dir_log)

        remove_file_cmd = f"rm -rf {self.logfile}"
        remove_file_result = self.primary_root.sh(remove_file_cmd).result()
        self.logger.info(remove_file_result)
        remove_test_file_cmd = f"ls {self.logfile}"
        remove_test_file_log = self.primary_root.sh(remove_test_file_cmd).result()
        self.logger.info(remove_test_file_log)

        self.logger.info('-----断言tearDown-----')
        self.assertEquals(show_log_min_msg, self.log_min_msg_default,
                          '执行失败' + text)
        self.assertEquals(show_log_statement, self.log_stat_default,
                          '执行失败' + text)
        self.assertEquals(show_collector, self.collector_default,
                          '执行失败' + text)
        self.assertEquals(show_log_dir, self.log_dir_default,
                          '执行失败' + text)
        self.assertEquals(show_debug_parse, self.debug_parse_default,
                          '执行失败' + text)
        self.assertEquals(show_log_des, self.log_des_default,
                          '执行失败' + text)
        self.assertIn("No such file or directory", remove_test_dir_log, '执行失败:' + text_remove)
        self.assertIn("No such file or directory", remove_test_file_log, '执行失败:' + text_remove)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
